package com.sdkx.wear_os.customview;


import static com.sdkx.wear_os.utils.DisplayUtilsKt.dpToPx;

import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter4.BaseQuickAdapter;
import com.sdkx.wear_os.R;
import com.sdkx.wear_os.adapter.PopupWindowAdapter;
import com.sdkx.wear_os.bean.PopupBean;

import java.util.ArrayList;
import java.util.List;

/**
 * popupWindow弹窗
 */
public class PopupWindowView {

    View viewItem = null;
    RecyclerView rcvPopupWindowList;
    ImageView popupIv;
    PopupWindow pullDownView;// 弹出窗口
    private List<PopupBean> popupLists = new ArrayList<PopupBean>();
    private PopupWindowAdapter mPopupWindowAdapter;
    private Context mContext;
    private PopupListener mPopupListener;
    private int maxLine = 6;

    /**
     * @param width 宽度
     * @param type  0 无角 1 角在中间  2角在右边 3角在左边
     */
    public PopupWindowView(Context mContext, int width, String type) {
        this.mContext = mContext;
        LayoutInflater inflater = LayoutInflater.from(mContext);
        if (type.equals("0")) {
            viewItem = inflater.inflate(R.layout.popup_view, null);
        } else if (type.equals("1")) {
            viewItem = inflater.inflate(R.layout.popup_view1, null);
        } else if (type.equals("2")) {
            viewItem = inflater.inflate(R.layout.popup_view2, null);
        } else {
            viewItem = inflater.inflate(R.layout.popup_view3, null);
        }
        rcvPopupWindowList = viewItem.findViewById(R.id.popup_recyclerview);
        mPopupWindowAdapter = new PopupWindowAdapter();
        rcvPopupWindowList.setLayoutManager(new LinearLayoutManager(mContext));
        rcvPopupWindowList.setAdapter(mPopupWindowAdapter);
        pullDownView = new PopupWindow(viewItem, width,
                LinearLayout.LayoutParams.WRAP_CONTENT, true);
        pullDownView.setOutsideTouchable(true);
        pullDownView.setClippingEnabled(false);
        mPopupWindowAdapter.setOnItemClickListener((baseQuickAdapter, view, position) -> {
            if (mPopupListener != null) {
                mPopupListener.onItemClick(position, PopupWindowView.this);
            }
        });
    }

    /**
     * 设置下拉框的数据
     */
    public void initPopupData(PopupListener popupListener, Float dp) {
        mPopupListener = popupListener;
        if (mPopupListener != null) {
            mPopupListener.initPopupData(popupLists);
        }
        if (popupLists != null && popupLists.size() <= maxLine) {
            pullDownView.setHeight(dpToPx(mContext, 40f) * popupLists.size() + dpToPx(mContext,
                    dp));
        } else {
            assert popupLists != null;
            pullDownView.setHeight(dpToPx(mContext, 40f) * popupLists.size() * maxLine + dpToPx(mContext, 10f));
        }
        if (mPopupWindowAdapter != null) {
            mPopupWindowAdapter.submitList(popupLists);
        }
    }

    /**
     * 设置最大行popupWindow
     */
    public void setMaxLines(int maxLines) {
        maxLine = maxLines;
    }

    /**
     * 显示popupWindow
     */
    public void showing(View v) {
        pullDownView.showAsDropDown(v, 0, 0);
    }

    /**
     * 显示popupWindow
     */
    public void showingLocation(View v, int yHeight) {
        pullDownView.setAnimationStyle(R.style.PopupAnimation);
        DisplayMetrics dm = Resources.getSystem().getDisplayMetrics();
        // 屏幕宽度(像素)
        int width = dm.widthPixels;
        pullDownView.showAtLocation(v, Gravity.TOP | Gravity.END,
                width - (int) v.getX() - v.getWidth(),
                (int) v.getY() + v.getHeight() + dpToPx(mContext, yHeight));
        pullDownView.update();
    }

    public void showingActivity(View v) {
        pullDownView.setAnimationStyle(R.style.PopupAnimation);
        DisplayMetrics dm = Resources.getSystem().getDisplayMetrics();
        // 屏幕宽度(像素)
        int width = dm.widthPixels;
        pullDownView.showAtLocation(v, Gravity.TOP | Gravity.END,
                width - (int) v.getX() - v.getWidth(),
                (int) v.getY() + v.getHeight() + dpToPx(v.getContext(), 24));
        pullDownView.update();
    }

    /**
     * 显示popupWindow
     */
    public void showingLocationCenter(View v) {
        pullDownView.setAnimationStyle(R.style.PopupAnimation);
        DisplayMetrics dm = Resources.getSystem().getDisplayMetrics();
        // 屏幕宽度(像素)
        int width = dm.widthPixels;
        pullDownView.showAtLocation(v, Gravity.TOP | Gravity.CENTER, 0,
                (int) v.getY() + v.getHeight());
        pullDownView.update();
    }

    /**
     * @param isDismissNb 是否隐藏导航栏
     */
    public void showAsDropDown(View v, int xoff, int yoff, Boolean isDismissNb) {
        if (isDismissNb) {
            pullDownView.setFocusable(false);
            pullDownView.showAsDropDown(v, xoff, yoff);
            pullDownView.getContentView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        } else {
            pullDownView.showAsDropDown(v, xoff, yoff);
        }
    }

    /**
     * 关闭popupWindow
     */
    public void dismiss() {
        pullDownView.dismiss();
    }

    /**
     * 获取popupWindow
     */
    public PopupWindow getPullDownView() {
        return pullDownView;
    }

    /**
     * 获取选择的PopuBean
     */
    public PopupBean getPopupBean(int popupPosition) {
        return popupLists.get(popupPosition);
    }

    /**
     * 获取选择的名称
     */
    public String getTitle(int popupPosition) {
        return popupLists.get(popupPosition).getTitle();
    }

    /**
     * 获取选择的id
     */
    public Object getId(int popupPosition) {
        return popupLists.get(popupPosition).getId();
    }

    /**
     * 获取选择的imageId
     */
    public Object getImageId(int popupPosition) {
        return popupLists.get(popupPosition).getImageId();
    }

}